<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form
        ref="dataFrom"
        :inline="true"
        :model="dataFrom"
        class="demo-form-inline"
      >
        <el-form-item label="项目名称">
          <el-input
            v-model="dataFrom.name"
            clearable
            placeholder="项目名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="getDataListByName"
          >查询
          </el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="dialogFormVisible = true">
            新增
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 角色数据表单 -->
      <el-table ref="multipleTable" :data="dataList" border style="width: 100%">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="项目编号" prop="projectId" width="80">
        </el-table-column>
        <el-table-column label="项目名称" prop="project.name" width="150">
        </el-table-column>
        <el-table-column label="项目类型" prop="project.type" width="130">
        </el-table-column>
        <el-table-column label="项目审核状态" prop="status" width="120">
        </el-table-column>
        <el-table-column label="项目创建人" prop="user.username" width="150">
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180">
        </el-table-column>
        <el-table-column
          label="最近一次的修改时间"
          prop="updateTime"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="lookEdit(scope.$index, scope.row)"
            >
              查看项目
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleChangeProject(scope.$index, scope.row)"
            >
              修改项目
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除项目
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看打开的界面 -->
      <el-dialog
        :model="formLook"
        :visible.sync="dialogFormVisibleLook"
        title="项目详情"
        width="50%"
      >
        <el-descriptions title="项目信息">
          <el-descriptions-item label="项目编号">
            {{ this.formLook.project.id }}
          </el-descriptions-item>
          <el-descriptions-item label="项目名称">
            {{ this.formLook.project.name }}
          </el-descriptions-item>
          <el-descriptions-item label="项目类型">
            {{ this.formLook.project.type }}
          </el-descriptions-item>
          <el-descriptions-item label="项目状态">{{
              this.formLook.status
            }}
          </el-descriptions-item>

          <el-descriptions-item label="创建人">
            <el-tag size="small">{{ this.formLook.user.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="项目说明">{{
              this.formLook.project.remark
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions>
          <el-descriptions-item :span="1" label="创建时间">{{
              this.formLook.createTime
            }}
          </el-descriptions-item>
          <el-descriptions-item
            :span="1"
            label="最近一次修改时间"
            prop="updateTime"
          >{{ this.formLook.updateTime }}
          </el-descriptions-item
          >
        </el-descriptions>


        <el-descriptions>
          <el-descriptions-item :span="1" label="附件">
            <div id="files">
              <ul style="margin-left: -5%">
                <li v-for="(item,index) in this.filesJSON " style="list-style:none;float: left">
                  <el-button @click="openFile(item)">
                    {{ index }}
                  </el-button>
                </li>
                <li v-if="this.filesJSON===null" style="list-style:none;float: left;margin-top: -10%"><span>空</span>
                </li>
              </ul>
            </div>
          </el-descriptions-item>
        </el-descriptions>


        <el-divider></el-divider>
        <el-descriptions title="项目创建者信息">
          <el-descriptions-item label="账号">
            {{ this.formLook.user.username }}
          </el-descriptions-item
          >
          <el-descriptions-item label="姓名">
            <el-tag size="small">{{ this.formLook.user.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="邮箱">{{
              this.formLook.user.email
            }}
          </el-descriptions-item>
          <el-descriptions-item label="手机号码">{{
              this.formLook.user.mobile
            }}
          </el-descriptions-item>
          <el-descriptions-item label="账号状态">{{
              this.formLook.user.status === 1 ? "激活" : "禁用"
            }}
          </el-descriptions-item>
        </el-descriptions>
      </el-dialog>
      <!-- 新增按钮打开的页面 -->
      <el-dialog :visible.sync="dialogFormVisible" title="创建项目" width="35%">
        <el-form :model="form">
          <el-form-item label="项目名称" label-width="120px">
            <el-input
              v-model="form.project.name"
              autocomplete="off"
              placeholder="项目名称"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目内容" label-width="120px">
            <el-input
              v-model="form.project.content"
              autocomplete="off"
              placeholder="项目内容"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目类型" label-width="120px">
            <el-input
              v-model="form.project.type"
              autocomplete="off"
              placeholder="项目类型"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="申请资金" label-width="120px">
            <el-input
              v-model="form.project.money"
              autocomplete="off"
              placeholder="申请资金"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目说明" label-width="120px">
            <el-input
              v-model="form.project.remark"
              style="width: 300px"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="checkItemBeforeInsert()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑按钮打开的页面 -->
      <el-dialog
        :visible.sync="dialogFormVisibleChange"
        title="编辑项目内容"
        top="10px"
        width="35%"
      >
        <el-form :model="formChange">
          <el-form-item label="项目名称" label-width="120px">
            <el-input
              v-model="formChange.project.name"
              autocomplete="off"
              placeholder="账号"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目内容" label-width="120px">
            <el-input
              v-model="formChange.project.content"
              autocomplete="off"
              placeholder="项目内容"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目类型" label-width="120px">
            <el-input
              v-model="formChange.project.type"
              autocomplete="off"
              placeholder="项目类型"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="申请资金" label-width="120px">
            <el-input
              v-model="formChange.project.money"
              autocomplete="off"
              placeholder="申请资金"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目说明" label-width="120px">
            <el-input
              v-model="formChange.project.remark"
              style="width: 300px"
              type="textarea"
            ></el-input>
          </el-form-item>
          <el-form-item label="上传附件" label-width="120px">
            <el-button @click="changeDialogFormVisibleFile()">上传附件</el-button>
            <el-button v-if="fileList.length>this.fileListed" type="primary" @click="uploadFile">提交</el-button>
            <!--            :http-request="uploadFile"-->
            <el-upload
              v-if="this.dialogFormVisibleFile"
              :auto-upload="false"
              :before-upload="handleBeforeUpload"
              :file-list="fileList"
              :limit="fileLimit"
              :multiple="true"
              :on-change="handleChange"
              :on-exceed="handleExceed"
              :show-file-list="true"
              action="#"
              drag
              list-type="text"
              name="file"
              style="margin-top: 10px;"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div slot="tip" class="el-upload__tip">只能上传doc/xls/ppt文件，且不超过{{ this.fileSize }}kb</div>
            </el-upload>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleChange = false">取 消</el-button>
          <el-button type="primary" @click="handleUpdate()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>
export default {
  name: "MyProject",
  data() {
    return {
      username: sessionStorage.getItem("username"),
      dataFrom: {
        name: "",
      },
      //新增
      form: {
        project: {
          name: "",
          content: "",
          type: "",
          money: "",
          mobile: "",
        },
        user: {
          username: "",
        },
      },
      //查看
      formLook: {
        project: {},
        user: {},
      },
      //编辑
      formChange: {
        id: "",
        project: {
          id: "",
          name: "",
          content: "",
          type: "",
          money: "",
          remark: "",
          filesJson: [],
        },
        user: {
          username: "",
        },
      },
      dataList: [], //页面展示的数据集合
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,
      dialogFormVisible: false, //默认关闭新建用户界面
      dialogFormVisibleChange: false, //默认关闭编辑用户界面
      dialogFormVisibleLook: false,
      dialogFormVisibleFile: false,//上传文件
      dataListFrom: "getDataList",//当前数据来源于搜索还是全局
      fileList: [], //上传文件，以数组方式上传
      fileListed: 0, //上传过的文件
      fileLimit: 5, //文件上传个数限制
      fileSize: 10,//文件限制大小
      filesJSON: [],//回显附件路径
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.SizeOrCurrentChangeAfter();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.SizeOrCurrentChangeAfter();
    },
    //在更改数组大小或者页号前，判断数据来源
    SizeOrCurrentChangeAfter() {
      if (this.dataListFrom === "getDataList")
        this.getDataList();
      else this.getDataListByName();
    },
    //打开附件
    openFile(item) {
      // const reader = new FileReader();
      // reader.readAsText(item);
    },
    //是否显示上传框
    changeDialogFormVisibleFile() {
      this.dialogFormVisibleFile = !this.dialogFormVisibleFile
    },
    //上传前的校验
    handleBeforeUpload(file) {
      const uploadTypes = [
        "doc",
        "docx",
        "xls",
        "xlsx",
        "ppt",
        "pptx",
      ];
      const filetype = file.name.replace(/.+\./, "");
      const isRightSize = (file.size || 0) / 1024 / 1024 < this.fileSize;
      if (!isRightSize) {
        this.$message.error("文件大小超过 " + this.fileSize + "MB");
        return false;
      }
      if (uploadTypes.indexOf(filetype.toLowerCase()) === -1) {
        this.$message.warning({
          message:
            "请上传后缀名为doc,docx,xls,xlsx,ppt,pptx的附件",
        });
        return false;
      }
      return true;
    },
    //文件数量改变
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    //超出文件个数的回调
    handleExceed() {
      this.$message({
        type: "warning",
        message: "超出最大上传文件数量的限制！",
      });
    },
    //上传附件
    uploadFile() {
      //上传文件的需要FormData类型;所以要转
      const FormDates = new FormData();
      this.fileList.forEach((val) => {
        FormDates.append("files", val.raw);
      });
      this.$http.post("/upload/file",
          FormDates, {
          headers: {
            "Content-Type": "",
          }
        }
      )
        .then((res) => {
          this.fileListed = this.fileList.length
          this.formChange.project.filesJson = res.data.data.slice()
        });
    },
    //检查创建项目时是否有未填项
    checkItemBeforeInsert() {
      if (this.form.project.money === "") {
        this.$confirm("当前申请资金为0,是否继续创建项目？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.handleInsert()
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消创建项目",
          });
        });
      } else {
        this.handleInsert()
      }
    },
    //新增页面的确认键
    handleInsert() {
      this.$http.post("/item/insert", this.form).then(() => {
        this.dialogFormVisible = false; //关闭窗口
        this.getDataList();
      });
    },
    //编辑页面的确认键
    handleUpdate() {
      this.$confirm("是否确认修改?修改后项目将回到未审核状态！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const data = {
          itemBo: this.formChange,
          pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
        }
        this.$http.post("/item/update", data).then((res) => {
          this.dialogFormVisibleChange = false;
          this.cutDataList(res);
        });
      });
    },
    //查询所有数据
    getDataList() {
      this.dataListFrom = "getDataList";
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/item/getList/MyItem", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },
    //查询单条数据
    getDataListByName() {
      const name = this.dataFrom.name
      //搜索条件不为空
      if (name !== "") {
        this.dataListFrom = "getDataListByName";
        const data = {
          name: name,
          pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
        }
        this.$http.post("/item/getList/MyItem/search", data).then((res) => {
          this.cutDataList(res);
        });
      } else {
        this.getDataList();
      }
    },
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },
    //查看按钮
    lookEdit(index, item) {
      //查找项目的附件
      this.searchFiles(item)
      this.dialogFormVisibleLook = true; //打开项目详细界面
      //回显数据
      this.formLook = item;
    },
    //查找项目的附件
    searchFiles(item) {
      this.filesJSON = JSON.parse(item.project.files)
    },
    //编辑按钮
    handleChangeProject(index, item) {
      this.dialogFormVisibleChange = true; //打开编辑界面
      //回显数据
      this.formChange.project = item.project;
      this.formChange.id = item.id;
      //清空上传列表
      this.fileList = [];
      this.fileListed = 0
      this.dialogFormVisibleFile = false;//上传文件
    },
    //删除按钮
    handleDelete(index, item) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            itemId: item.id,
            projectId: item.projectId,
            pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
          }
          this.$http.post("/item/delete", data).then(() => {
            this.getDataList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getDataList();
  },
};
</script>
